package com.lm.caffeine.service;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


@Service
public class UserService {

    @Cacheable(value = "usersCache", key = "#id",unless = "#result==null")
    public String getUserById(String id) {
        System.out.println("查询数据库...");
        return "User-" + id;
    }

    /**
     * 更新缓存
     */
    @CachePut(cacheNames = "usersCache", key = "#id",unless = "#result==null")
    public String updateUser(String id) {
        System.out.println("更新数据库...");
        return "User-" + id;
    }

    /**
     * 删除缓存
     */
    @CacheEvict(cacheNames = "usersCache", key = "#id")
    public String delUser(String id) {
        System.out.println("删除数据库...");
        return "User-" + id;
    }
}